﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum GoldType
{
    up,
    down,
    left,
    right,
    seven,
    two,
    three
}

public class GoldBoxComponent : MonoBehaviour
{
    public float speed = 100f;
    GoldType type;
    public GameObject Icon;
    Vector3 target;
    //bool IsDIe = false;
    Animator animator;
    public GameObject Parts;
    bool IsDie = false;
    Vector3 pos = new Vector3();
    List<GameObject> PartsList = new List<GameObject>();
    // Update is called once per frame
    private void OnEnable()
    {
        pos = transform.position;
        animator = GetComponent<Animator>();
        Icon.SetActive(true);
        Parts.SetActive(false);
        foreach(GameObject obj in PartsList)
        {
            obj.SetActive(true);
        }
    }
    void Update()
    {
        if (GameConfig.IsGameOver)
            return;
        if (IsDie)
        {
            return;
        }
        if (Icon.GetComponent<GoldBoxIconComponent>().GetGlodBreak() == true)
        {
            animator.SetBool("Open", true);
            IsDie = true;
        }
        else
        {
            transform.Translate(Vector2.left * speed * Time.deltaTime);

        }
        if (transform.position.x < -77)
        {
            transform.parent.gameObject.SetActive(false);
        }
        if (Input.anyKeyDown)
        {
            foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
            {
                if (Input.GetKeyDown(keyCode))
                {
                   Icon.GetComponent<GoldBoxIconComponent>().IconBreak(keyCode);
                }
            }
        }
    }
    public void CoinDown()
    {
        AppFacde.Instance.SendNotification(GameEvents.ADD_MONEY,15);
        Debug.Log("飞行怪父物体名:" + transform.parent.name);
        GoldManager.Instance.GoldBoxExpolde(transform.position, 15,transform.parent);
        Parts.SetActive(true);
        Parts.transform.position = transform.position;
        PartComponent[] objs = Parts.GetComponentsInChildren<PartComponent>();
        for(int i = 0; i < objs.Length; i++)
        {
            objs[i].transform.position = new Vector2(transform.position.x + UnityEngine.Random.Range(-200, 200), transform.position.y + UnityEngine.Random.Range(-200, 200));
            PartsList.Add(objs[i].gameObject);
        }
        Debug.Log("开始掉落硬币");
    }
    public void IconHide()
    {
        Icon.SetActive(false);
    }
    public void ObjHide()
    {
        IsDie = false;
        gameObject.transform.position = pos;
        gameObject.SetActive(false);
    }
}
